package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.utt.common.util.LocaleMessageSourceUtil;
import cn.com.utt.common.util.Tips;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.service.LogService;
import cn.com.utt.unms.vo.LogSee;

@RestController
@RequestMapping("v1")
public class LogController {
	
	@Autowired
    private LocaleMessageSourceUtil messageSourceUtil;
	@Autowired
	private LogService logService;

	@RequestMapping(value = "/queryLog", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public Map<String, Object> queryLog(@RequestBody TableInput input, Principal principal) {
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		RestResult result = new RestResult();
		TableOutput<LogSee> output = logService.queryLogByuserId(input, user.getId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("logSeelist", output.getList());
		return result.toJsonObject();

	}

	@RequestMapping(value = "/deleteLog", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryLog(Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		logService.delete(user.getId());
		result.setErrorMsg(messageSourceUtil.getMessage(Tips.OperationSucceed));
		return result.toJsonObject();

	}
}
